package com.zjitc.backend_shop.controller.User;

import com.zjitc.backend_shop.pojo.Category;
import com.zjitc.backend_shop.pojo.Goods;
import com.zjitc.backend_shop.service.CategoryService;
import com.zjitc.backend_shop.service.GoodsService;
import com.zjitc.backend_shop.utils.APIResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王新瑜
 * @date 2024/12/20 15:42
 */

@Controller
@RestController
@Tag(name = "客户端部分")
@RequestMapping("/home")
public class CategoryUserController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;
    @Operation(summary = "获取分类")
    @RequestMapping("/category/list")
    public APIResponse<List<Category>> findAll(HttpServletRequest request){
        List<Category> categories = categoryService.findAllCategory();
        APIResponse<List<Category> > apiResponse = new APIResponse<>(0,"",categories);
        return apiResponse;
    }
    @Operation(summary = "获取商品列表（无分页）")
    @RequestMapping("/goods/list")
    public APIResponse<List<Goods>> findAllGoods(HttpServletRequest request){
        List<Goods> goods = goodsService.findAllGoods();
        for (Goods good : goods) {
            if (!good.getPicture().equals("")) {
                good.setPicture(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + good.getPicture());
            }
        }
        APIResponse<List<Goods> > apiResponse = new APIResponse<>(0,"",goods);
        return apiResponse;
    }
}
